package com.topcent.tioxyc.xxljob;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.topcent.tioxyc.constants.KeyConstant;
import com.topcent.tioxyc.pojo.DeliveryVoucher;
import com.topcent.tioxyc.pojo.SalesCalculation;
import com.topcent.tioxyc.pojo.SalesOrderKu;
import com.topcent.tioxyc.pojo.vo.SalesCalculationyuFu;
import com.topcent.tioxyc.pojo.vo.XiaoSYing;
import com.topcent.tioxyc.util.ApiClient;
import com.topcent.tioxyc.util.ConnmeUtils;
import com.topcent.tioxyc.util.PurchaseCommunity;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author: weidian
 * @date: 2024/12/24 09:49
 * @Description: 收款单计算金额
 */
@Slf4j
@Component
public class ShoukuandanXxlJob {
    @XxlJob("shoukuandan")
    public String shoukuan() {
        List<SalesCalculation> fukuandans = new ArrayList<>();
        Header[] headers = ConnmeUtils.createHeaders();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", "669cb709a73cc1289c49800e");
        parameter.put("entry_id", "669cb70dab60aa3f4a7bb8b2");

        Map<String, Object> filter = new HashMap<>();
        filter.put("rel", "and");
        Map<String, Object> cond = new HashMap<>();
        cond.put("field", "flowState");
        cond.put("type", "flowstate");
        cond.put("method", "eq");
        List<Integer> values = new ArrayList<>();
        values.add(1);
        cond.put("value", values);
        List<Map<String, Object>> conds = new ArrayList<>();
        conds.add(cond);
        filter.put("cond", conds);
        parameter.put("filter", filter);

        boolean fale = true;
        String zh = "";
        do {
            if (!zh.isEmpty()) {
                parameter.put("data_id", zh);
            }
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<SalesCalculation> list = JSONUtil.toList(objects, SalesCalculation.class);
            if (list.size() == 100) {
                zh = list.getLast().get_id();
            } else {
                fale = false;
            }
            fukuandans.addAll(list);
        } while (fale);

        List<SalesCalculation> weiyu = new ArrayList<>();

        Map<String, Map<String, SalesCalculationyuFu>> yuFuMap = new HashMap<>();

        for (SalesCalculation fukuandan : fukuandans) {
            if (!fukuandan.get_widget_1721548605263().isEmpty() || !fukuandan.get_widget_1721546510495().isEmpty()) {
                weiyu.add(fukuandan);
                if (!fukuandan.get_widget_1721546510495().isEmpty()) {   //预付款
                    for (SalesCalculation.OrderPrepayment orderPrepayment : fukuandan.get_widget_1721546510495()) {
                        if (yuFuMap.containsKey(orderPrepayment.get_widget_1721546510504())) {
                            if (yuFuMap.get(orderPrepayment.get_widget_1721546510504()).containsKey(orderPrepayment.get_widget_1721616942683())) {
                                SalesCalculationyuFu salesCalculationyuFu = yuFuMap.get(orderPrepayment.get_widget_1721546510504()).get(orderPrepayment.get_widget_1721616942683());
                                salesCalculationyuFu.set_widget_1721546510520(salesCalculationyuFu.get_widget_1721546510520() + orderPrepayment.get_widget_1721546510520());
                                yuFuMap.get(orderPrepayment.get_widget_1721546510504()).put(orderPrepayment.get_widget_1721616942683(), salesCalculationyuFu);
                            } else {
                                SalesCalculationyuFu salesCalculationyuFu = new SalesCalculationyuFu();
                                salesCalculationyuFu.set_widget_1721546510520(orderPrepayment.get_widget_1721546510520());
                                yuFuMap.get(orderPrepayment.get_widget_1721546510504()).put(orderPrepayment.get_widget_1721616942683(), salesCalculationyuFu);
                            }
                        } else {
                            Map<String, SalesCalculationyuFu> wf = new HashMap<>();
                            SalesCalculationyuFu salesCalculationyuFu = new SalesCalculationyuFu();
                            salesCalculationyuFu.set_widget_1721546510520(orderPrepayment.get_widget_1721546510520());
                            wf.put(orderPrepayment.get_widget_1721616942683(), salesCalculationyuFu);
                            yuFuMap.put(orderPrepayment.get_widget_1721546510504(), wf);
                        }
                    }
                }

                if (!fukuandan.get_widget_1721548605263().isEmpty()) {  //尾款
                    for (SalesCalculation.OrderFinalPayment orderFinalPayment : fukuandan.get_widget_1721548605263()) {
                        if (yuFuMap.containsKey(orderFinalPayment.get_widget_1721548605264())) {
                            if (yuFuMap.get(orderFinalPayment.get_widget_1721548605264()).containsKey(orderFinalPayment.get_widget_1721616942684())) {
                                SalesCalculationyuFu salesCalculationyuFu = yuFuMap.get(orderFinalPayment.get_widget_1721548605264()).get(orderFinalPayment.get_widget_1721616942684());
                                salesCalculationyuFu.set_widget_1721548605266(salesCalculationyuFu.get_widget_1721548605266() + orderFinalPayment.get_widget_1721548605266());
                                yuFuMap.get(orderFinalPayment.get_widget_1721548605264()).put(orderFinalPayment.get_widget_1721616942684(), salesCalculationyuFu);
                            } else {
                                SalesCalculationyuFu salesCalculationyuFu = new SalesCalculationyuFu();
                                salesCalculationyuFu.set_widget_1721548605266(orderFinalPayment.get_widget_1721548605266());
                                yuFuMap.get(orderFinalPayment.get_widget_1721548605264()).put(orderFinalPayment.get_widget_1721616942684(), salesCalculationyuFu);
                            }
                        } else {
                            Map<String, SalesCalculationyuFu> wf = new HashMap<>();
                            SalesCalculationyuFu salesCalculationyuFu = new SalesCalculationyuFu();
                            salesCalculationyuFu.set_widget_1721548605266(orderFinalPayment.get_widget_1721548605266());
                            wf.put(orderFinalPayment.get_widget_1721616942684(), salesCalculationyuFu);
                            yuFuMap.put(orderFinalPayment.get_widget_1721548605264(), wf);
                        }
                    }
                }
            }
        }

        yuFuMap.forEach((key, value) -> {
            Map<String, Object> parameterCxv = new HashMap<>();
            parameterCxv.put("app_id", "667230ad22a8581756b77d52");
            parameterCxv.put("entry_id", "669e17aa09cd4f4e279d4d5d");
            HashMap<String, String> communti = new HashMap<>();
            communti.put("_widget_1718759603973", key);
            List<SalesOrderKu> salesOrderKus = PurchaseCommunity.queryBased(parameterCxv, headers, communti, SalesOrderKu.class);
            if (salesOrderKus != null && !salesOrderKus.isEmpty()) {
                double[] zong = {0}; // 使用数组存储值
                value.forEach((key1, value1) -> {
                    for (SalesOrderKu.OrderDetail orderDetail : salesOrderKus.getFirst().get_widget_1718759603981()) {
                        if (orderDetail.get_widget_1718759603995().equals(key1)) {
                            orderDetail.set_widget_1721982279364(value1.get_widget_1721546510520());
                            orderDetail.set_widget_1721982279365(value1.get_widget_1721548605266());
                            orderDetail.set_widget_1722241817628(value1.get_widget_1721546510520() + value1.get_widget_1721548605266());
                            zong[0] += value1.get_widget_1721546510520() + value1.get_widget_1721548605266()-1;
                        }
                    }

                });
                double i = salesOrderKus.getFirst().get_widget_1718759603984() - zong[0];
                if (i<=2) {
                    // 修改出库凭证
                    salesOrderKus.getFirst().set_widget_1734504746939("是");
                    Map<String, Object> parametereefs = new HashMap<>();
                    parametereefs.put("app_id", "669cb709a73cc1289c49800e");
                    parametereefs.put("entry_id", "674d839475ae49b66713aadb");
                    HashMap<String, String> communtid = new HashMap<>();
                    communtid.put("_widget_1731918876993", key);
                    List<DeliveryVoucher> deliveryVouchers = PurchaseCommunity.queryBased(parametereefs, headers, communtid, DeliveryVoucher.class);
                    if (deliveryVouchers != null && !deliveryVouchers.isEmpty()) {
                        for (DeliveryVoucher deliveryVoucher : deliveryVouchers) {
                            deliveryVoucher.set_widget_1734429312703("是");
                            Map<String, Object> parameterInster = new HashMap<>();
                            parameterInster.put("app_id", "669cb709a73cc1289c49800e");
                            parameterInster.put("entry_id", "674d839475ae49b66713aadb");
                            parameterInster.put("is_start_trigger", "true");
                            parameterInster.put("data_id", deliveryVoucher.get_id());
                            deliveryVoucher.set_id(null);
                            parameterInster.put("data", ConnmeUtils.convertEntityToMapNull(deliveryVoucher));
                            String jsonSalee = JSONUtil.toJsonStr(parameterInster);
                            ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSalee);
                        }
                    }
                    // 修改销售订单引用表的是否收款
                    Map<String, Object> parametereefsyy = new HashMap<>();
                    parametereefsyy.put("app_id", "667230ad22a8581756b77d52");
                    parametereefsyy.put("entry_id", "66a30b55ecb3bf2d5748208c");
                    HashMap<String, String> communtidyy = new HashMap<>();
                    communtidyy.put("_widget_1718759603973", key);
                    List<XiaoSYing> xiaoSYings = PurchaseCommunity.queryBased(parametereefsyy, headers, communtidyy, XiaoSYing.class);
                    if (xiaoSYings != null && !xiaoSYings.isEmpty()) {
                        for (XiaoSYing xiaoSYing : xiaoSYings) {
                            xiaoSYing.set_widget_1734673959092("是");
                            Map<String, Object> parameterInster = new HashMap<>();
                            parameterInster.put("app_id", "667230ad22a8581756b77d52");
                            parameterInster.put("entry_id", "66a30b55ecb3bf2d5748208c");
                            parameterInster.put("is_start_trigger", "true");
                            parameterInster.put("data_id", xiaoSYing.get_id());
                            xiaoSYing.set_id(null);
                            parameterInster.put("data", ConnmeUtils.convertEntityToMapNull(xiaoSYing));
                            String jsonSalee = JSONUtil.toJsonStr(parameterInster);
                            ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSalee);
                        }
                    }


                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", "667230ad22a8581756b77d52");
                    parameterInster.put("entry_id", "669e17aa09cd4f4e279d4d5d");
                    parameterInster.put("is_start_trigger", "true");
                    parameterInster.put("data_id", salesOrderKus.getFirst().get_id());
                    salesOrderKus.getFirst().set_id(null);
                    salesOrderKus.getFirst().set_widget_1718759603978(null);
                    salesOrderKus.getFirst().set_widget_1718759603990(null);

                    for (SalesOrderKu.OrderDetail orderDetail : salesOrderKus.getFirst().get_widget_1718759603981()) {
                        Optional.ofNullable(orderDetail.get_widget_1726803260151())
                                .ifPresent(valuey -> orderDetail.set_widget_1726803260151(valuey.toString()));

                    }
                    SalesOrderKu salesOrderKu = ConnmeUtils.updateFields(salesOrderKus.getFirst());

                    parameterInster.put("data", ConnmeUtils.convertEntityToMapNull(salesOrderKu));
                    String jsonSale = JSONUtil.toJsonStr(parameterInster);
                    ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSale);

                }
            }
        });
        return "suu===" + fukuandans.size() + "预付尾款不为空的：" + weiyu.size();
    }
}
